package com.lelefly.base.utils;

import com.lelefly.base.po.UserAuthEntity;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by lelefly on 2017/7/30.
 */
public class AuthUtils {

    public static boolean isAuthed(JdbcTemplate jdbcTemplate, String tel, String type) {
        String sql = "select * from user_auth where tel=? and type=?";
        try {
            UserAuthEntity authEntity = jdbcTemplate.queryForObject(sql, new Object[]{tel, type}, new RowMapper<UserAuthEntity>() {
                @Override
                public UserAuthEntity mapRow(ResultSet resultSet, int i) throws SQLException {
                    UserAuthEntity entity = new UserAuthEntity();
                    entity.setId(resultSet.getInt(1));
                    entity.setTel(resultSet.getString(2));
                    entity.setType(resultSet.getString(3));
                    entity.setValue(resultSet.getString(4));
                    return  entity;
                }
            });
            if (authEntity != null && "1".equals(authEntity.getValue())) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;

    }
}
